import React,{Component} from 'react'
import {bindActionCreators} from'redux'
import PropTypes from 'prop-types'
import {connect} from 'react-redux'
import FriendList from '../components/FriendList'
import FriendChat from '../components/FriendChat'
import * as TodoActions from '../actions';

const Friend = ({login,friend, actions,timeState={timer:null}}) => (friend.isOpenChat?(<div>
  <FriendChat 
    sendMessage = { actions.sendMessage } 
    friendState = { friend }
    setIsOpenChat = { actions.setIsOpenChat }
    sendChatMessage = {actions.sendChatMessage}
    fetchFriendMessage = {actions.fetchFriendMessage}
    login = {login}
    timeState = {timeState}
  />
  </div>):(
  <div>
    <FriendList 
      login ={login}
      refreshFriendList = { actions.refreshFriendList } 
      startFriendChat = { actions.startFriendChat } 
      friendList = { friend.friendList }
      friendState = {friend}
      setIsOpenChat = { actions.setIsOpenChat }
      deleteFriendFetch = {actions.deleteFriend}
      fetchFriendMessage = {actions.fetchFriendMessage}
      timeState = {timeState}
    />
  </div>
))

Friend.propTypes = {
  friend:PropTypes.object.isRequired,
  actions:PropTypes.object.isRequired,
}

const mapStateToProps = state =>({     //把state的部分东东给到组件，并以属性方式访问
    friend:state.friend,
    login:state.login
})

const mapDispatchToProps = dispatch => {  //把全局reducer的函数（自动分发信息）给到组件，并以属性方式访问（挂载在组件属性中）
    console.log(TodoActions);
    return {actions:bindActionCreators(TodoActions, dispatch)}
}


export default connect(mapStateToProps, mapDispatchToProps)(Friend)
